package com.choosefine.it.worker.service.Impl;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.worker.dao.WorkerCenterMapper;
import com.choosefine.it.worker.model.FindWork;
import com.choosefine.it.worker.service.WorkerCenterService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

@Service("workerCenterService")
public class WorkerCenterServiceImpl implements WorkerCenterService {
	
	@Resource
	private WorkerCenterMapper workerCenterMapper;
	@Autowired
	private MongoTemplate mongoTemplate;
	
	//@Override
	public UserInfo findUserSingleDetailById(String userCode) throws BusinessException {
        UserInfo userInfo = mongoTemplate.findOne(new Query(Criteria.where("userCode").is(userCode)), UserInfo.class);
        if(userInfo!=null){
        userInfo.setStayYear(DateUtils.getworkeryear(userInfo.getCtime()));
        }
        return userInfo;
		//return workerCenterMapper.findUserSingleDetailById(user.getUserCode());
	}

	/*private long getStayYeay(Long ctime){
	   return 	((System.currentTimeMillis()-ctime)/1000/86400);
	}*/

	@Override
	public Page<FindWork> queryPublishWorkMgt(Map<String, Object> param,PageDTO page) throws BusinessException {
		//数据校验
//		if(StringUtils.isEmpty(param.get(CommonConstant.USER_CODE))){
//			throw new BusinessException(ResultCode.FAILED, CommonConstant.WORKER_USERINFO_USERID_IS_NULL);
//		}
		//条件追加
		if (CommonConstant.ORDER_ASC.equalsIgnoreCase(page.getOrderBy())) {
			param.put("orderBy", "fw.ctime asc ,fw.is_valid asc");
		}else{
			param.put("orderBy", "fw.ctime desc ,fw.is_valid asc");
		}
		//分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		//查询从哪行开始取offset条记录
		Page<FindWork> findWorks = workerCenterMapper.queryPublishWorkMgt(param);

		for (int i = 0; i < findWorks.size(); i++) {
			findWorks.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(findWorks.get(i).getId())), FindWork.class));
		}

		param = null;
		return findWorks;
	}

	@Override
	public int updateUserInfo(UserInfo user) throws BusinessException {
		checkData(user);
		int num = 0;
		if (!StringUtils.isEmpty(user.getSkillsCcie())) {
			num = workerCenterMapper.updateWorkerKill(user);
		}
		
		if (!StringUtils.isEmpty(user.getRegistAddress()) 
				|| !StringUtils.isEmpty(user.getContactAddress())
				|| !StringUtils.isEmpty(user.getPhoneNumber())){
			num = workerCenterMapper.updateUserInfo(user);
		}
		return num;
	}

	/**
	 * 校验数据传输的有效性
	 * @param user
	 * @throws BusinessException 
	 */
	private void checkData(UserInfo user) throws BusinessException {
		if (StringUtils.isEmpty(user.getUserCode())) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.WORKER_USERINFO_USERID_IS_NULL);
		}
	}

	@Override
	public int insertPublishWork(FindWork findWork) throws BusinessException {
		if (null == findWork.getExpectSalary() || findWork.getExpectSalary().intValue() == 0 ) {
			throw new BusinessException(ResultCode.FAILED, String.format(CommonConstant.COMMON_IS_VALID,"Expect Salary"));
		}
		if (StringUtils.isEmpty(findWork.getUserCode())) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.WORKER_USERINFO_USERID_IS_NULL);
		}
		//第二版工种id 为多个，改为String 类型 —— update by juchengyang 2017/2/14 
		if (StringUtils.isEmpty(findWork.getWorkTypeId()) || findWork.getWorkTypeId().split(",").length == 0) {
			throw new BusinessException(ResultCode.FAILED, String.format(CommonConstant.COMMON_IS_VALID, "Work Type"));
		}
		/**
		 * publisher 字段，第二版变更为操作员编号，故废弃次逻辑
		 */
//		if (StringUtils.isEmpty(findWork.getPublisher())) {
//			findWork.setPublisher(findWork.getUserName());
//		}
		findWork.setCtime(System.currentTimeMillis());
		findWork.setPublishTime(System.currentTimeMillis());
		findWork.setReleaseNum("WFK" + System.currentTimeMillis());
		findWork.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		findWork.setPublishStatus(0);

		int isNum = workerCenterMapper.insertPublishWork(findWork);
		if (isNum > 0) {
			//发布工作,存储明细
			UserInfo userInfo= mongoTemplate.findOne(new Query(Criteria.where("userCode").is(findWork.getUserCode())),UserInfo.class);
			findWork.setUserInfo(userInfo);
			mongoTemplate.insert(findWork);
		}
		return isNum;
	}
}